package router

import (
	. "api/controller"
	. "api/middleware"
	"github.com/gin-gonic/gin"
	"github.com/unrolled/secure"
)

func InitRouter(env string) *gin.Engine {
	router := gin.Default()

	router.MaxMultipartMemory = 1 << 20

	router.Use(Cors())

	if env != "dev" && env != "test" {
		router.Use(TlsHandler())
	}

	v1 := router.Group("/v1")
	v1.Use(AuthToken())
	{
		v1.POST("/register", Register)
		v1.POST("/login", Login)
		v1.POST("/forgot_password", ForgotPassword)
		v1.GET("/vcode", GetVcode)

		// 用户
		v1.POST("/customer", CreateCustomer)
		v1.POST("/customer_record", CreateCustomerRecord)

		// 证券信用卡开户
		v1.GET("/securities_company", GetSecuritiesCompany)
		v1.GET("/securities_company_details", GetSecuritiesCompanyDetails)
		v1.GET("/credit_card", GetCreditCard)
		v1.GET("/credit_card_details", GetCreditCardDetails)
		v1.POST("/auth_user", AuthUser)

		// 推广官
		v1.GET("/promotion_officer_info", GetPromotionOfficerInfo)
		v1.POST("/withdraw", CreateWithdraw)
		v1.GET("/apprentices", GetApprentices)
		v1.GET("/income_record", GetIncomeRecord)
		v1.GET("/withdraw_record", GetWithdrawRecord)

		// 工禧云
		v1.GET("/maker_auth_info", GetMakerAuthInfo)
		v1.GET("/maker_auth_url", GetMakerAuthUrl)
		v1.GET("/maker_sign_url", GetMakerSignUrl)
	}

	upload := router.Group("/v1/upload")
	upload.Use(AuthAdminToken())
	{
		upload.POST("/single", UploadSingleFile)
		upload.POST("/xlsx", ImportExcel)
	}

	admin := router.Group("/v1/admin")
	admin.Use(AuthAdminToken())
	{
		// 登录
		admin.POST("/login", SyserLogin)

		// 收入支出
		admin.GET("/income_pay", GetIncomePay)

		// 证券开户
		admin.GET("/securities_company", GetSecuritiesCompany)
		admin.POST("/securities_company", CreateSecuritiesCompany)
		admin.PUT("/securities_company", UpdateSecuritiesCompany)
		admin.DELETE("/securities_company", DeleteSecuritiesCompany)

		// 信用卡
		admin.GET("/credit_card", GetCreditCard)
		admin.POST("/credit_card", CreateCreditCard)
		admin.PUT("/credit_card", UpdateCreditCard)
		admin.DELETE("/credit_card", DeleteCreditCard)

		// 推广官
		admin.GET("/promotion_officer", GetPromotionOfficer)
		admin.POST("/promotion_officer", CreatePromotionOfficer)
		admin.DELETE("/promotion_officer", DeletePromotionOfficer)
		admin.GET("/apprentices", GetPromotionApprentices)
		admin.DELETE("/apprentices", DeletePromotionApprentices)

		// 提现管理
		admin.GET("/withdraw", GetWithdraw)
		admin.PUT("/withdraw", UpdateWithdraw)
		admin.PUT("/withdraw_state", UpdateWithdrawState)

		// 白名单
		admin.GET("/white_list", GetWhiteList)
		admin.POST("/white_list", CreateWhite)
		admin.PUT("/white_list", UpdateWhite)

		//  充值
		admin.GET("/charge", GetCharge)
		admin.POST("/charge", CreateCharge)
		admin.PUT("/charge", UpdateCharge)
	}

	return router
}

func TlsHandler() gin.HandlerFunc {
    return func(c *gin.Context) {
        secureMiddleware := secure.New(secure.Options{SSLRedirect: true})

        err := secureMiddleware.Process(c.Writer, c.Request)
        if err != nil {
            c.Abort()
            return
		}

        c.Next()
    }
}